package cn.wolfcode.crm.util;

import cn.wolfcode.crm.domain.Employee;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * 用户上下文工具类
 * 此类的方法已不需要再用 因为登录登出 已经交给shiro处理
 */
public class UserContext {

    //从shiro中 获取当前用户
    public static Employee getCurrentUser() {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();//获取用户信息 认证时传入的第一个参数
        return employee;
    }

   /*
   //用户的权限表达式集合 在Session中的名称
    public static final String EXPRESSIONS_IN_SESSION = "EXPRESSIONS_IN_SESSION";
    //当前用户在 Session 中的名称
    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";

    //获取session对象
    public static HttpSession getSession() {
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    //把当前用户 存到 session中
    public static void setCurrentUser(Employee employee) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION, employee);
    }


    //从Session中 获取当前用户
    public static Employee getCurrentUser() {
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    //从Session中获取用户权限列表
    public static List<String> getExpressions() {
        return (List<String>) getSession().getAttribute(EXPRESSIONS_IN_SESSION);
    }

    //把用户权限列表 存到 Session中
    public static void setExpressions(List<String> permissionExpressions) {
        getSession().setAttribute(EXPRESSIONS_IN_SESSION, permissionExpressions);
    }*/


}
